import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('./views/Home.vue'),
      children: [
        {
          path: '/categories',
          name: 'categories',
          component: () => import('@/components/shop/categories.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/components/Login.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    next()
    return
  }
  if (localStorage.getItem('token')) {
    next()
  } else {
    next('/login')
  }
})
export default router;
